package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.CircleThemeDetailAdapter;
import com.keyancompany.zhongbaopingtai.api.CircleAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.CommentModel;
import com.keyancompany.zhongbaopingtai.model.SearchRecordModel;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.AnswerPopup;
import com.keyancompany.zhongbaopingtai.weight.IListView;
import com.keyancompany.zhongbaopingtai.weight.MenuPopuWindow;

import java.util.List;

import butterknife.Bind;

public class FriendsCircleThemeDetailActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.list_view)
    IListView listView;
    @Bind(R.id.find_swipe_layout)
    SwipeRefreshLayout swipeLayout;
    @Bind(R.id.empty_hint)
    TextView emptyHint;
    @Bind(R.id.empty_view)
    LinearLayout emptyView;

    SearchRecordModel currentModel;
    CircleThemeDetailAdapter themeAdapter;
    private AnswerPopup answerPopup;
    private int total, currentPage;
    private String pid;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_circle_theme_detail;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, "话题");
    }

    @Override
    protected void setUpViews() {
        pid = getIntent().getExtras().getString(Constants.TEAM_ID);
        currentModel = (SearchRecordModel) getIntent().getExtras().getSerializable(Constants.ITEM);

        themeAdapter = new CircleThemeDetailAdapter(this);
        themeAdapter.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = (int) view.getTag();
                sendContent(position);
//                IntentUtils.startActivity(mContext, CircleThemeDetailActivity.class, Constants.ITEM, model);
            }
        });
        listView.setAdapter(themeAdapter);
        listView.setStateListener(new IListView.SwipeLayoutState() {
            @Override
            public boolean isRefresh() {
                return swipeLayout.isRefreshing();
            }

            @Override
            public boolean isLastPage() {
                return total == currentPage;
            }

            @Override
            public void requestNextPage() {
                requestTask(++currentPage);
            }
        });
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));
        swipeLayout.setOnRefreshListener(this);
    }


    void sendContent(int position) {
        final String answerId = position == 0 ? "" : themeAdapter.getItem(position).getAnswerid();
        if (answerPopup == null) {
            answerPopup = new AnswerPopup(mContext);
            answerPopup.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
            answerPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            answerPopup.setCliclSureListener(new MenuPopuWindow.ClickSureListener() {
                @Override
                public void clickSureLinstener(String... param) {
                    CircleAPI.fscRequest(pid, param[0], answerId, new IAsyncHttpResponseHandler() {
                        @Override
                        public void start(int type) {

                        }

                        @Override
                        public void success(int type, String json) {
                            if (validateJson(json).isSuccess()) {
                                answerPopup.clearContent();
                                answerPopup.dismiss();

                                onRefresh();
                            }
                        }

                        @Override
                        public void failed(String error) {

                        }

                        @Override
                        public void finish(int type) {

                        }
                    }, getLoading(R.string.watting_hint));
                }
            });
        }
        answerPopup.showAtLocation(listView, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        requestTask(currentPage);
    }

    public void requestTask(int currentPage) {

        CircleAPI.plsearchRecord(pid, currentPage + "", this, getLoading(-1));
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        swipeLayout.setRefreshing(false);
        if (currentPage == 1) {
            themeAdapter.clear();
//            mAdapter.addAllAfter();
        }
        listView.loadMoreFinish();
        BaseModel base = validateJson(json);
        if (base.isSuccess()) {
            JSONObject result = JSON.parseObject(base.data);
            CommentModel first = JSONObject.parseObject(base.data, CommentModel.class);
            themeAdapter.addItemsAfter(first);
            total = result.getInteger("pageCount");
            if (total == currentPage)
                listView.removeFooterView();
            List<CommentModel> items = JSONArray.parseArray(result.getString(Constants.LIST), CommentModel.class);
            themeAdapter.addAllAfter(items);
            isEmpty();
        } else {
            T.showShort(mContext, base.msg);
        }


    }

    public void isEmpty() {
        if (themeAdapter.getCount() < 1) {
            emptyView.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        swipeLayout.setRefreshing(false);
    }
}
